package com.ts.oa.controller;

import com.google.common.collect.Maps;
import com.ts.common.constant.CommonConstant;
import com.ts.common.controller.BaseController;
import com.ts.common.domain.DictDO;
import com.ts.common.service.DictService;
import com.ts.common.utils.PageInfos;
import com.ts.common.utils.PageUtils;
import com.ts.common.utils.Query;
import com.ts.common.utils.Result;
import com.ts.oa.domain.NotifyDO;
import com.ts.oa.domain.NotifyRecordDO;
import com.ts.oa.service.NotifyRecordService;
import com.ts.oa.service.NotifyService;
import com.ts.system.domain.UserDO;
import com.ts.system.util.SystemUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知通告
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2017-10-05 17:11:16
 */

@Controller
@RequestMapping("/oa/notify")
public class NotifyController extends BaseController {
    @Autowired
    private NotifyService notifyService;
    @Autowired
    private NotifyRecordService notifyRecordService;
    @Autowired
    private DictService dictService;

    @GetMapping()
    @RequiresPermissions("oa:notify:notify")
    String oaNotify() {
        return "oa/notify/notify";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("oa:notify:notify")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        PageUtils pageUtils = new PageInfos(params) {
            @Override
            public List inject(Map params) {
                List<Map<String, Object>> inputsList = notifyService.list(params);
                return inputsList;
            }
        }.query().lowerUnderscore();
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("oa:notify:add")
    String add() {
        return "oa/notify/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("oa:notify:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", id);
        List<Map<String, Object>> notifies = notifyService.list(params);
        Map<String, Object> notify = notifies.get(0) == null ? new HashMap<>() : notifies.get(0);
        Object userIdsObj = notify.get("user_ids");
        String userNames = "";
        if(userIdsObj !=null){
            String[] userIds = userIdsObj.toString().split(",");
            for (int i = 0; i < userIds.length; i++) {
                UserDO userDO = SystemUtil.getUsers().get(Long.valueOf(userIds[i]));
                if (userDO != null) {
                    if (i == 0) {
                        userNames = userNames.concat(SystemUtil.getUsers().get(Long.valueOf(userIds[i])).getName());
                    } else {
                        userNames = userNames.concat(",").concat(SystemUtil.getUsers().get(Long.valueOf(userIds[i])).getName());
                    }
                }
            }
        }

        notify.put("userIds", notify.get("user_ids"));
        notify.put("userNames", userNames);
        model.addAttribute("notify", notify);
        return "oa/notify/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("oa:notify:add")
    public Result save(NotifyDO notify) {
        if (CommonConstant.DEMO_ACCOUNT.equals(getUsername())) {
            return Result.error(Result.FAIL, "演示系统不允许修改,完整体验请部署程序");
        }

        notify.setCreateBy(getUserId());
        if (notifyService.save(notify) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("oa:notify:edit")
    public Result update(NotifyDO notify) {
        if (CommonConstant.DEMO_ACCOUNT.equals(getUsername())) {
            return Result.error(Result.FAIL, "演示系统不允许修改,完整体验请部署程序");
        }
        notifyService.update(notify);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("oa:notify:remove")
    public Result remove(Long id) {
        if (CommonConstant.DEMO_ACCOUNT.equals(getUsername())) {
            return Result.error(Result.FAIL, "演示系统不允许修改,完整体验请部署程序");
        }
        if (notifyService.remove(id) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("oa:notify:batchRemove")
    public Result remove(@RequestParam("ids[]") Long[] ids) {
        if (CommonConstant.DEMO_ACCOUNT.equals(getUsername())) {
            return Result.error(Result.FAIL, "演示系统不允许修改,完整体验请部署程序");
        }
        notifyService.batchRemove(ids);
        return Result.ok();
    }

    @ResponseBody
    @GetMapping("/message")
    PageUtils message() {
        Map<String, Object> params = new HashMap<>(16);
        params.put("offset", 0);
        params.put("limit", 3);
        Query query = new Query(params);
        query.put("userId", getUserId());
        query.put("isRead", CommonConstant.OA_NOTIFY_READ_NO);
        return notifyService.selfList(query);
    }

    @GetMapping("/selfNotify")
    String selefNotify() {
        return "oa/notify/selfNotify";
    }

    @ResponseBody
    @GetMapping("/selfList")
    PageUtils selfList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        query.put("userId", getUserId());
        return notifyService.selfList(query);
    }

    @GetMapping("/read/{id}")
    String read(@PathVariable("id") Long id, Model model) {
        NotifyDO notify = notifyService.get(id);
        //更改阅读状态
        NotifyRecordDO notifyRecordDO = new NotifyRecordDO();
        notifyRecordDO.setNotifyId(id);
        notifyRecordDO.setUserId(getUserId());
        notifyRecordDO.setReadDate(new Date());
        notifyRecordDO.setIsRead(CommonConstant.OA_NOTIFY_READ_YES);
        notifyRecordService.changeRead(notifyRecordDO);
        model.addAttribute("notify", notify);
        return "oa/notify/read";
    }


}
